const env = {
	'dev': 'https://xxxxxxx.com',
	'test': 'http://118.31.37.53:8008/api/',
	'beta': 'https://xxxxxxx.com',
	'prod': 'https://xxxxxxx.com',
}


// 全局请求封装
const timeout = 180000;
const baseURL = 'https://p2papi.gratls.com.cn/'
// 全局请求封装
function upload(config) {
	let header = {};
	if (uni.getStorageSync("token")) {
		header['Authorization'] = 'Bearer ' + uni.getStorageSync("token");
		// header['token'] = 'Bearer ' + uni.getStorageSync("token");
		// header['token'] = '' + uni.getStorageSync("token");
	} else {
		// uni.setStorageSync('token','Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjEwMDI5LCJ1TmFtZSI6IjEzNDg3MzAyNDE0Iiwic2NvcGUiOjgsImV4cCI6MTY5OTY2MzA0NSwiaWF0IjoxNjk5NTkxMDQ1fQ.zueTMpHL_dUaFYUtSo22fxtwkfLVTmyM1JCQ_iHi7gY')
		// header['Authorization'] = 'Bearer ' + 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjEwMDI5LCJ1TmFtZSI6IjEzNDg3MzAyNDE0Iiwic2NvcGUiOjgsImV4cCI6MTY5OTU4NTAxOCwiaWF0IjoxNjk5NTEzMDE4fQ.B0xCUfEZJsVKoj8ImdrONV4mEoYUxCiIHRfIE1i_z8s';
	}
	return new Promise((resolve, reject) => {
		uni.uploadFile(Object.assign(config, {
			url: baseURL + config.url,
			method: !config.method ? 'GET' : config.method.toLocaleUpperCase(),
			header: header,
			name: 'file[]',
			filePath: config.data.file,
			formData:config.data.info,
			// formData: {
				// type_id: 1,
				// user_id: 5
				// money: 1000,
				// outer_notice_sn: '2231231231',
				// file: config.data.file
			// },
			timeout: timeout, //超时时间
			withCredentials: true, //跨域请求时是否携带凭证（cookies）
			success: (res) => {
				// console.log(res);
				let statusCode = res.statusCode;
				//异常判断
				if (statusCode !== 200 && statusCode !== 204) {
					uni.showToast({
						title: res.data.error ? res.data.error.message : '网络错误!',
						icon: 'none',
						duration: 2000
					});
					if (statusCode == 401) {
						uni.setStorageSync('islogin', false)
						setTimeout(function() {
							uni.reLaunch({
								url: '/pages/index/index',
							})
						}, 2000)
						return
					}
					//返回异常信息
					reject(res.data || res.data.error.message);
				} else {
					// console.log(res.data);
					var status = res.data.success
					var code = res.data.code
					if (!status && code=='401') {
						uni.setStorageSync('islogin', false)
						uni.setStorageSync('token', '')
						setTimeout(function() {
							uni.reLaunch({
								url: '/pages/index/index',
							})
						}, 2000)
						return
					}
					//返回正常信息
					resolve(res.data);
				}
			},
			fail: (error) => {
				//抛出错误
				console.log('err', JSON.stringify(error))
				uni.showToast({
					title: error.message,
					icon: 'none',
					duration: 2000
				});
				reject(error);
			}
		}));
	})
}
export default upload